//节点类
class Node{
    constructor(element){
        this.element = element;
        this.prev = null;
        this.next = null;
    }
}

class DoubleList{
    constructor(){
        this.head = null;  //首节点
        this.tail = null;  //尾节点
        this.count = 0; //记录节点数量
    }
    //push  从末尾添加
    push(element){
        let node = new Node(element);
        if(this.head === null){
            //链表为空
            this.head = node;
            this.tail = node;
        }else{
            //链表不为空
            let last = this.tail;
            last.next = node;
            node.prev = last;
            this.tail = node;
        }
    }

    //找指定的节点
    getNode(index){
        let current =this.head;
        for(let i = 0;i<index;i++){
            current = current.next;
        }
        return current
    }

    //插入
    /**
     * @param element 插入的元素
     * @param index   下标
     */
    insert(element,index){
        //边界值的判断
        if(index >= 0 && index <= this.count){
            let node = new Node(element);
            //在边界值内
            if(index === 0){
                //1.在开头插入
                if(this.head === null){
                    //1.1链表为空
                    this.head = node;
                    this.tail = node;
                }else{
                    //1.2链表不为空
                    let current = this.head;
                    node.next = current;
                    current.prev = node;
                    this.head = node;
                }
            }else if( index=== this.count){
                //2.在末尾插入
                let last = this.tail;
                last.next = node;
                node.prev = last;
                this.tail = node;
            }else{
                //3.在中间插入
                let current = this.getNode(index);
                let prev = current.prev;

                prev.next = node;
                node.prev = prev;

                node.next = current;
                current.prev = node;
            }
            this.count++;
        }else{
            return false
        }
    }

    //remove  通过下标
    removeAt(index){
        if(index >= 0 && index < this.count){
            if(index === 0){
                //删除开头节点
                if(this.count === 1){
                    //只有一个节点
                    this.head = null;
                    this.tail = null;
                }else{
                    //不止一个节点
                    let current = this.head;
                    this.head = current.next;
                    current.next.prev = null;
                }
            }else if(index === (this.count-1)){
                //删除末尾节点
                let last = this.tail;
                last.prev.next = null;
                this.tail = last.prev;
            }else{
                //删除中间节点
                let current = this.getNode(index);
                let prev = current.prev;
                prev.next = current.next;
                current.next.prev= prev;
            }
            this.count--;
        }else{
            return undefined
        }
    }

    indexOf(element){
        let current = this.head;
        for(let i = 0;i<this.count;i++){
            if(element === current.element){
                return i
            }else{
                current = current.next;
            }
        }
        return -1
    }
}

let d = new DoubleList();

d.insert('a',0);

d.insert('b',0); //b  a

d.insert('c',1);

d.removeAt(1);

console.log(d.indexOf('b'))

console.log(d); //b c a